package com.jyu.apts.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.jyu.apts.dto.ItemProcessDto;
import com.jyu.apts.dto.ItemProcessRecordDto;
import com.jyu.apts.dto.ItemProcessRecordInfoDto;
import com.jyu.apts.dto.ItemProcessRecordTemplateInfoDto;
import com.jyu.apts.dto.QRCodeDto;
import com.jyu.apts.dto.TableDto;
import com.jyu.apts.entity.ItemBatchRecord;
import com.jyu.apts.entity.ItemProcess;
import com.jyu.apts.entity.ItemProcessRecord;
import com.jyu.apts.entity.ItemProcessRecordTemplateInfo;
import com.jyu.apts.entity.QRCode;
import com.jyu.apts.entityEnum.QRCodeTypeEnum;
import com.jyu.apts.service.ItemProcessRecordService;
import com.jyu.apts.service.ItemProcessRecordTemplateInfoService;
import com.jyu.apts.service.ItemProcessService;
import com.jyu.apts.service.QRCodeService;

@RestController
public class OriginController {

	@Autowired
	QRCodeService qrCodeService;
	@Autowired
	ItemProcessRecordService itemProcessRecordService;
	@Autowired
	ItemProcessService itemProcessService;
	@Autowired
	ItemProcessRecordTemplateInfoService itemProcessRecordTemplateInfoService;
	
	@RequestMapping("/getQrCodeInfo")
	public ModelAndView getQrCodeInfo(long id){
		QRCode qrCode = qrCodeService.getOne(id);
		if(qrCode == null) {
			return null;
		}
		else {
			ModelAndView mv = new ModelAndView();
			mv.setViewName("/originInfo.html");
			mv.addObject("qrCode", new QRCodeDto().convert(qrCode));
			return mv;
		}
	}
	
	@RequestMapping("/getNormalQrCodeInfoByItemProcess")
	public ModelAndView getNormalQrCodeInfoByItemProcess(long id,long itemProcessId){
		ItemProcess itemProcess = itemProcessService.getOne(itemProcessId);
		QRCode qrCode = qrCodeService.getOne(id);
		if(qrCode == null) {
			return null;
		}
		else {
			ModelAndView mv = new ModelAndView();
			mv.setViewName("/originInfo.html");
			mv.addObject("qrCode", new QRCodeDto().convert(qrCode));
			mv.addObject("itemProcess",new ItemProcessDto().convert(itemProcess));
			return mv;
		}
	}
	
	@RequestMapping("/originItemProcessRecord")
	public ModelAndView originItemProcessRecord(String originCode){
		QRCode qrCode = qrCodeService.findByCode(originCode);
		if(qrCode == null) {
			return null;
		}
		else if(qrCode.getType()!=QRCodeTypeEnum.ORIGIN.getType()) {
			return null;
		}
		ItemBatchRecord itemBatchRecord = qrCode.getItemBatchRecord();
		List<ItemProcessRecordDto> itemProcessRecordDtos = new ArrayList<ItemProcessRecordDto>();
		List<ItemProcessRecord> itemProcessRecords = itemBatchRecord.getItemProcessRecords();
		itemProcessRecords.forEach(itemProcessRecord->itemProcessRecordDtos.add(new ItemProcessRecordDto().convert(itemProcessRecord)));
		ModelAndView mv = new ModelAndView();
		mv.setViewName("/originItemProcessRecord.html");
		mv.addObject("itemProcessRecords", itemProcessRecordDtos);
		return mv;
	}
	
	@RequestMapping("/originItemProcessRecordInfo")
	public ModelAndView originItemProcessRecordInfo(long itemProcessRecordId){
		ItemProcessRecord itemProcessRecord = itemProcessRecordService.getOne(itemProcessRecordId);
		if(itemProcessRecord == null) {
			return null;
		}
		else {
			List<ItemProcessRecordInfoDto> itemProcessRecordInfoDtos = new ArrayList<ItemProcessRecordInfoDto>();
			itemProcessRecord.getItemProcessRecordInfos().forEach(itemProcessRecordInfo->itemProcessRecordInfoDtos.add(new ItemProcessRecordInfoDto().convert(itemProcessRecordInfo)));
			ModelAndView mv = new ModelAndView();
			mv.setViewName("/originItemProcessRecordInfo.html");
			mv.addObject("itemProcessRecord", new ItemProcessRecordDto().convert(itemProcessRecord));
			mv.addObject("itemProcessRecordInfos", itemProcessRecordInfoDtos);
			return mv;
		}
	}
	
	@RequestMapping("/originItemProcessRecordTemplateInfo_html")
	public ModelAndView originItemProcessRecordTemplateInfo(long itemProcessRecordId){
		ModelAndView mv = new ModelAndView();
		mv.setViewName("/originItemProcessRecordTemplateInfo.html");
		mv.addObject("itemProcessRecordId", itemProcessRecordId);
		return mv;
	}
	
	@RequestMapping("/originItemProcessRecordTemplateInfoPage")
	public TableDto<ItemProcessRecordTemplateInfoDto> getItemProcessRecordTemplateInfoPage(Integer pageNumber,Integer pageSize,Integer status,String search,long itemProcessRecordId) {
		Page<ItemProcessRecordTemplateInfo> pageResult = itemProcessRecordTemplateInfoService.getItemProcessRecordTemplateInfoPage(pageNumber, pageSize, search, null, itemProcessRecordId);
		List<ItemProcessRecordTemplateInfo> itemProcessRecordTemplateInfos = pageResult.getContent();
		List<ItemProcessRecordTemplateInfoDto> itemProcessRecordTemplateInfoDtos = new ArrayList<ItemProcessRecordTemplateInfoDto>();
		itemProcessRecordTemplateInfos.forEach(itemProcessRecordTemplateInfo->itemProcessRecordTemplateInfoDtos.add(new ItemProcessRecordTemplateInfoDto().convert(itemProcessRecordTemplateInfo)));
		return new TableDto<ItemProcessRecordTemplateInfoDto>(itemProcessRecordTemplateInfoDtos,pageResult.getTotalElements());
	}
}
